<?php 

/**
 * 货品列表管理控制器
 * @author 阿柱 <422852954@qq.com>
 */
class GoodsListController extends AuthController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K('GoodsList');
	}
	
    /**
     *   **列表展示方法
     */
    public function index(){
    	if(IS_POST){
    		$spec=Q("post.spec"); 
			foreach ($spec as $k => $v) {
				if(!$v)  $this->error('颜色或尺寸不能为空');
			}
//			验证货号是否存在
			$num=$this->model->_number();
			if($this->model->where("number='{$num}'")->find())  $this->error('货号已存在');
//			自动验证
			if(!$this->model->create())   $this->error($this->model->error);
//			执行添加
			$this->model->addGoods();
			$this->success('添加成功','',0.5);
			
    	}
    	$gid=Q('get.gid',0,'intval');
//		(规格atype=1)
		$specData=M()->join('__goods__ g JOIN __type_attr__ ta ON g.tid=ta.tid')->where("ta.atype=1 AND g.gid={$gid}")->all();
//		给数组绑定一个规格字段
		foreach ($specData as $k => $v) {
			$where="taid=".$v['taid']." AND gid={$gid}";
//			p($where);
			$specData[$k]['spec']=K('GoodsAttr')->where("{$where}")->all();
		}
//		分配规格
		$this->assign('specData',$specData);
		
//		分配列表数据
		$data=$this->model->where("gid={$gid}")->all();
		foreach ($data as $k => $v) {
			$arr=explode(',', $v['combine']);
//			查询组合属性中的ID对应的属性值
			foreach ($arr as $key => $value) {
				static $spec=array();
				$where="gaid=".$value." AND gid={$gid}";
//				p($where);
				$spec[$key]=K('GoodsAttr')->where("{$where}")->find();	
			}
			$data[$k]['spec']=$spec;
		}
		$this->assign('data',$data);
        $this->display(); 
    }
	/**
	 * 删除货品
	 */
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->del();
		$this->success('删除成功');
	}
	/**
	 * 编辑货品
	 */
	public function edit(){
		if(IS_POST){
//			p(Q("post."));die;
			$model=$this->model;
			if(!$model->editGoods())  $this->error($model->error);
			$this->success('修改成功');
		}
		$glid=Q('get.glid',0,'intval');
		$oldData=M()->join('__goods_list__ gl JOIN __goods__ g ON gl.gid=g.gid JOIN __type_attr__ ta ON g.tid=ta.tid')->where("ta.atype=1 AND gl.glid={$glid}")->all();
		foreach ($oldData as $k => $v) {
			$arr=explode(',', $v['combine']);  
//			查询组合属性中的ID对应的属性值
			$where="gaid=".$arr[$k]." AND gid={$v['gid']}";
			$oldData[$k]['spec']=K('GoodsAttr')->where("{$where}")->find();	
		}
		$this->assign('oldData',$oldData);
		$this->display();
	}

}
 

 
 
 
 
 
 





 ?>